// by Xeno
#define __DEBUG__
#define THIS_FILE "fn_createsecondary.sqf"
#include "x_setup.sqf"

#define __getPos \
_poss = [_posarx, _mtradius] call FUNC(GetRanPointCircleBig);\
if (count _poss > 0 && {isOnRoad _poss}) then {_poss = []};\
while {count _poss == 0} do {\
	_poss = [_posarx, _mtradius] call FUNC(GetRanPointCircleBig);\
	if (count _poss > 0 && {isOnRoad _poss}) then {_poss = []};\
}

#define __specops \
__GetEGrp(_newgroup)\
_unit_array = [#specops, GVAR(enemy_side)] call FUNC(getunitliste);\
[_poss, _unit_array select 0, _newgroup,true] spawn FUNC(makemgroup);\
sleep 1.0112;\
_newgroup allowFleeing 0;\
_newgroup setVariable [QGVAR(defend), true]; \
[_newgroup, _poss] spawn FUNC(taskDefend); \
[_newgroup, 1] call FUNC(setGState); \
if (GVAR(doRespawnGroups)) then { \
	GVAR(reduce_groups) set [count GVAR(reduce_groups), _newgroup]; \
};

private ["_poss", "_newgroup", "_vehicle", "_mtmhandle", "_dummy", "_act2", "_nrcamps", "_i", "_wf", "_flagPole", "_trg_center", "_posarx"];
if !(call FUNC(checkSHC)) exitWith {};
__TRACE_1("","_this")

PARAMS_3(_wp_array,_mtradius,_trg_center);

sleep 3.120;

_mtmhandle = _wp_array execVM "x_mtmissions\x_getmtmission.sqf";

waitUntil {sleep 0.321;scriptDone _mtmhandle};
__TRACE("MT Mission Done")

if (GVAR(MTPriorityObjective) == 0) then {
	[QGVAR(priority_position),[]] call FUNC(NetSetJIP);
	GVAR(priority_resolved) = false;
	_mtmhandle = _wp_array execVM "x_mtmissions\x_getmtpriority.sqf";
	waitUntil {sleep 0.321;scriptDone _mtmhandle};
	__TRACE("MT Priority Done")
};

sleep 5.0123;

__TargetInfo

_posarx = _trg_center;
__getPos;
_vehicle = objNull;
// TYRGHEN - Allows to spawn more than the tower as target
if (typename GVAR(radio_tower) == typename []) then {
	private ["_obj","_new_pos"];
	{
		if (isNull _vehicle) then {
			_obj = createVehicle [_x, _poss, [], 0, "NONE"];
			_vehicle = _obj;
			_vehicle allowDamage false;
			_vehicle setPos _poss;
			_vehicle setVectorUp [0,0,1];
		} else {
			_new_pos = _poss findEmptyPosition [1,7,_x];
			if (count _new_pos <= 0) then {
				_new_pos = _poss;
			};
			_obj = createVehicle [_x, _new_pos, [], 0, "CAN_COLLIDE"];
			_obj addEventHandler ["handleDamage", {_this call FUNC(CheckMTShotHD)}];
			if (_obj isKindOf "LandVehicle") then {
				_obj lock true;
				_obj setFuel 0;
				_obj setAmmoCargo 0;
				_obj setFuelCargo 0;
				_obj setRepairCargo 0;
			};
		};
		sleep 1.2356;
	} foreach GVAR(radio_tower);
} else {
	_vehicle = createVehicle [GVAR(radio_tower), _poss, [], 0, "NONE"];
	_vehicle allowDamage false;
	_vehicle setPos _poss;
	_vehicle setVectorUp [0,0,1];
};
__TRACE_2("Tower Created","_vehicle","_poss")
[_vehicle] call FUNC(CheckMTHardTarget);
_vehicle allowDamage true;
[QGVAR(mt_radio_down),false] call FUNC(NetSetJIP);
["mt_radio_pos",_poss] call FUNC(NetSetJIP);
// TYRGHEN
if (GVAR(towerpointmarker) != "") then {
	[QGVAR(main_target_radiotower), _poss,"ICON","ColorBlack",[0.5,0.5],(localize "STR_DOM_MISSIONSTRING_521"),0,GVAR(towerpointmarker)] call FUNC(CreateMarkerGlobal);
};

#ifndef __TT__
[QGVAR(kbmsg), [9]] call FUNC(NetCallEventCTS);
#else
[QGVAR(kbmsg), [10]] call FUNC(NetCallEventCTS);
#endif
sleep 1.0112;
__specops;

sleep 5.234;
_dummy = GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index));
_current_target_pos = _dummy select 0;
_current_target_radius = _dummy select 2;
_act2 = GVAR(enemy_side_trigger) + " D";
GVAR(mt_spotted) = false;
GVAR(create_new_paras) = false;
#ifndef __TT__
GVAR(f_check_trigger) = [_current_target_pos, [_current_target_radius + 300, _current_target_radius + 300, 0, false], [GVAR(own_side_trigger), _act2, false], ["this", "0 = 0 spawn {if (!d_create_new_paras) then {d_create_new_paras = true;0 execFSM 'fsms\Parahandler.fsm'};d_mt_spotted = true;['d_kbmsg', [12]] call d_fnc_NetCallEventCTS;sleep 5;deleteVehicle d_f_check_trigger}", ""]] call FUNC(CreateTrigger);
#else
GVAR(f_check_trigger2) = objNull;
GVAR(f_check_trigger) = [_current_target_pos, [_current_target_radius + 300, _current_target_radius + 300, 0, false], ["WEST", _act2, false], ["this", "0 = 0 spawn {if (!d_create_new_paras) then {d_create_new_paras = true;0 execFSM 'fsms\Parahandler.fsm'};d_mt_spotted = true;['d_kbmsg', [13]] call d_fnc_NetCallEventCTS;sleep 5;deleteVehicle d_f_check_trigger;if (!isNull d_f_check_trigger2) then {deleteVehicle d_f_check_trigger2}}", ""]] call FUNC(CreateTrigger);
GVAR(f_check_trigger2) = [_current_target_pos, [_current_target_radius + 300, _current_target_radius + 300, 0, false], ["EAST", _act2, false], ["this", "0 = 0 spawn {if (!d_create_new_paras) then {d_create_new_paras = true;0 execFSM 'fsms\Parahandler.fsm'};d_mt_spotted = true;['d_kbmsg', [14]] call d_fnc_NetCallEventCTS;sleep 5;deleteVehicle d_f_check_trigger2;if (!isNull d_f_check_trigger) then {deleteVehicle d_f_check_trigger}}", ""]] call FUNC(CreateTrigger);
GVAR(f_check_trigger) = [_current_target_pos, [_current_target_radius + 300, _current_target_radius + 300, 0, false], ["WEST", _act2, false], ["this", "0 = 0 spawn {if (!d_create_new_paras) then {d_create_new_paras = true};d_mt_spotted = true;['d_kbmsg', [13]] call d_fnc_NetCallEventCTS;sleep 5;deleteVehicle d_f_check_trigger;if (!isNull d_f_check_trigger2) then {deleteVehicle d_f_check_trigger2}}", ""]] call FUNC(CreateTrigger);
GVAR(f_check_trigger2) = [_current_target_pos, [_current_target_radius + 300, _current_target_radius + 300, 0, false], ["EAST", _act2, false], ["this", "0 = 0 spawn {if (!d_create_new_paras) then {d_create_new_paras = true};d_mt_spotted = true;['d_kbmsg', [14]] call d_fnc_NetCallEventCTS;sleep 5;deleteVehicle d_f_check_trigger2;if (!isNull d_f_check_trigger) then {deleteVehicle d_f_check_trigger}}", ""]] call FUNC(CreateTrigger);
#endif

sleep 5.234;
_d_currentcamps = [];
_nrcamps = ceil random 5;
if (_nrcamps < 3) then {_nrcamps = 3};
_ctype = GVAR(wcamp);

GVAR(sum_camps) = _nrcamps;
if (!isServer) then {
	[QGVAR(sSetVar), [QGVAR(sum_camps), _nrcamps]] call FUNC(NetCallEventCTS);
};
_posarx = _trg_center;
for "_i" from 1 to _nrcamps do {
	__getPos;
	_wf = createVehicle [_ctype, _poss, [], 0, "NONE"];
	_wf setDir floor random 360;
	_svec = sizeOf _ctype;
	_xcountx = 0;
	_isFlat = [];
	while {_xcountx < 99} do {
		_isFlat = (position _wf) isFlatEmpty [_svec / 2, 100, 0.7, _svec, 0, false, _wf];
		// TYRGHEN - Added check to be above water level
		if (count _isFlat > 0 && {(_isFlat select 2) >= 0}) then {
			_isFlat set [2, 0];
			if (!isOnRoad _isFlat) then {
				_poss = _isFlat;
			} else {
				_isFlat = [];
			};
			if (count _isFlat > 0 && {count _d_currentcamps > 0}) then {
				{
					if (_wf distance _x < 50) exitWith {
						_isFlat = [];
					};
				} forEach _d_currentcamps;
			};
		};
		if (count _isFlat > 0) exitWith {};
		__INC(_xcountx);
	};
	_poss set [2, 0];
	_wf setPos _poss;
	_d_currentcamps set [count _d_currentcamps, _wf];
	_wf setVariable [QGVAR(SIDE), GVAR(enemy_side), true];
	_wf setVariable [QGVAR(INDEX), _i, true];
	_wf setVariable [QGVAR(CAPTIME), 40 + (floor random 10), true];
	_wf setVariable [QGVAR(CURCAPTIME), 0, true];
	#ifndef __TT__
	_wf setVariable [QGVAR(CURCAPTURER), GVAR(own_side_trigger_alt)];
	#else
	_wf setVariable [QGVAR(CURCAPTURER), ""];
	#endif
	_wf setVariable [QGVAR(STALL), false, true];
	_wf setVariable [QGVAR(TARGET_MID_POS), _trg_center];
	//_flagPole = createVehicle [(call FUNC(GetEnemyFlagType)), position _wf, [], 0, "NONE"];
	_fwfpos = _wf modelToWorld GVAR(flag_offset);
	//_fwfpos set [2, 4.3];
	_flagPole = createVehicle ["FlagPole_F", _fwfpos, [], 0, "NONE"];
	_flagPole setPos _fwfpos;
	_wf setVariable [QGVAR(FLAG), _flagPole, true];
	_maname = format[QGVAR(camp%1),_i];
	_wf setVariable [QGVAR(markername), _maname];
	// TYRGHEN
	if (GVAR(strongpointmarker) != "") then {
		[_maname, _poss,"ICON","ColorBlack",[0.5,0.5],"",0,GVAR(strongpointmarker)] call FUNC(CreateMarkerGlobal);
	};
	_flagPole setFlagTexture (call FUNC(getenemyflagtex));
	
	_wf addEventHandler ["HandleDamage", {0}];
	#ifndef __TT__
	[_wf, _flagPole] execFSM "fsms\HandleCamps2.fsm";
	#else
	[_wf, _flagPole] execFSM "fsms\HandleCampsTT2.fsm";
	#endif
	sleep 0.5;
};
[QGVAR(currentcamps),_d_currentcamps] call FUNC(NetSetJIP);
[QGVAR(campscaptured), 0] call FUNC(NetSetJIP);

#ifndef __TT__
[QGVAR(kbmsg), [15, _nrcamps]] call FUNC(NetCallEventCTS);
#else
[QGVAR(kbmsg), [16, _nrcamps]] call FUNC(NetCallEventCTS);
#endif

if (GVAR(with_mine_field) == 0 && random 100 > 80) then {
	[_mtradius,_trg_center] call FUNC(minefield);
};
if (GVAR(with_mine_roads)==0 && random 100 > 50) then {
	[_trg_center,(_mtradius * 0.7)] call FUNC(mineroads);
};

sleep 5.213;
GVAR(main_target_ready) = true;
// GVAR(fps_rescanNewObjects) = true;
// publicVariable QGVAR(fps_rescanNewObjects);
if (!isServer) then {
	[QGVAR(sSetVar), [QGVAR(main_target_ready), true]] call FUNC(NetCallEventCTS);
};
